package dice;

import java.awt.Button;
import java.awt.Component;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import main.DieRoll;
import main.UI;
import main.Unit;

public class SingleDie implements Runnable {

	public Integer modifyingNotifier = new Integer(0);
	
	public boolean mightModifiable = true;

	public ArrayList<Integer> dieValues;
	
	private Unit unitRolling;
	private int modifier = 0;
	private String label;
	private UI myUI;
	
	private int mightUsed = 0;
	
	private Label valueLabel;
	private Button useMight;
	private Label noteLabel;
	private Button doneModifying;
	
	private Button rerollButton;
	private boolean isRerollable = false;
	
	public void initialize(Unit unitRolling, int modifier, String label, UI myUI) {
		this.unitRolling = unitRolling;
		this.modifier = modifier;
		this.label = label;
		this.myUI = myUI;
	}
	
	@Override
	public void run() {
		System.out.println(1);
		
			// Need to:
			// Hijack UI's panel components (make stuff invisible)
			System.out.println(2);
			myUI.storePanel();
			
			// Roll die
			System.out.println(3);
			Integer dieValue = UI.rng.nextInt(6) + 1 + modifier;
			dieValue = Math.min(dieValue, 6);
			dieValue = Math.max(dieValue, 1);
			dieValues.add(dieValue);
			
			// Add might modification buttons and done modifying buttons
			System.out.println(4);
			ArrayList<Component> panel = new ArrayList<Component>();
			
			useMight = new Button("+");
			useMight.setName("dieRoll");
			useMight.setBounds(myUI.dim.width/2+20,myUI.dim.height/2,20,20);
			useMight.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					mightUsed += 1;
					unitRolling.useMight(1);
					valueLabel.setText((dieValues.get(0) + mightUsed) + " ");
					for (DieRoll roll : myUI.diceOnTable)
						roll.update();
					myUI.revalidate();
					myUI.repaint();
				}
			});
			panel.add(useMight);

			System.out.println(5);
			valueLabel = new Label(dieValue + " ");
			valueLabel.setName("dieRoll");
			valueLabel.setBounds(myUI.dim.width/2,myUI.dim.height/2,20,20);
			panel.add(valueLabel);

			System.out.println(6);
			noteLabel = new Label(this.label);
			noteLabel.setName("dieRoll");
			noteLabel.setBounds(myUI.dim.width/2+40,myUI.dim.height/2,100,20);
			panel.add(noteLabel);

			System.out.println(7);
			doneModifying = new Button("Done Modifying");
			doneModifying.setName("dieRoll");
			doneModifying.setBounds(myUI.dim.width/2+20,myUI.dim.height/2+30,100,20);
			doneModifying.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// doneModifying also synchronizes on this lock, and calls notify() to restart this Thread
					synchronized(modifyingNotifier) {
						modifyingNotifier.notify();
					}
				}
			});
			panel.add(doneModifying);

			System.out.println(8);
			myUI.setPanel(panel);
			
			
			
			// If not modifiable, can terminate
			if (dieValue >= 6 || unitRolling.might <= 0) {
				System.out.println(9);
				myUI.restorePanel();
			}
			// Otherwise, we can modify, so wait for a notification (from doneModifying)
			else {
				System.out.println(10);
				try {
					System.out.println(11);
					synchronized(modifyingNotifier){
						modifyingNotifier.wait();
					}
					System.out.println(12);
					myUI.restorePanel();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println(13);
		
		System.out.println(14);
	}

}
